package multithreading;

public class RunnableDemo implements Runnable{

    //买票并发
    private int ticketNums = 100;
    @Override
    public void run() {
        /*for (int i = 0; i < 20; i++) {
            System.out.println("实现runnable接口创建多线程"+Thread.currentThread().getName());
        }*/
        synchronized (this){
            while (ticketNums >0){
                System.out.println("线程"+Thread.currentThread().getName()+"第"+ticketNums--+"张票");
                try {
                    Thread.sleep(1000);//一千毫秒 等于一秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        RunnableDemo runnableDemo = new RunnableDemo();
        /*new Thread(runnableDemo).start();
        for (int i = 0; i < 20; i++) {
            System.out.println("主线程"+Thread.currentThread().getName());
        }*/
        //三个线程 去抢票 会出现问题
        new Thread(runnableDemo).start();
        new Thread(runnableDemo).start();
        new Thread(runnableDemo).start();
    }
}
